package org.metasketch.file;

import javax.swing.JOptionPane;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Rectangle;

import org.metasketch.demo.editor.DEMOEditor;
import org.metasketch.editor.IProjectHandler;
import org.metasketch.editor.perspectives.PerspectiveManager;
import org.metasketch.editor.timeline.SnapshotManager;

public class FileHandlingMenu extends MenuBar implements
		EventHandler<ActionEvent> {

	private DEMOEditor editor;

	private FileHandler handler;

	private IProjectHandler loader;

	private final int ITEM_PLAIN = 0; // Item types

	private final int ITEM_CHECK = 1;

	private final int ITEM_RADIO = 2;

	public MenuItem newProject;

	public MenuItem save;

	public MenuItem saveAs;

	public MenuItem load;

	private Menu file;

	public FileHandlingMenu(DEMOEditor editor, FileHandler handler,
			IProjectHandler loader) {

		this.editor = editor;
		this.handler = handler;
		this.loader = loader;

		file = new Menu("File");

		newProject = CreateMenuItem("New");
		save = CreateMenuItem("Save");
		saveAs = CreateMenuItem("Save As");
		load = CreateMenuItem("Load");

		file.getItems().addAll(newProject, save, saveAs, load);

		this.getMenus().add(file);

		// this.getMenus().add(new Rectangle());

		editor.getTop().setTop(this);

	}

	@Override
	public void handle(ActionEvent arg0) {
		System.out.println("TESTING!!!");
		// TODO Auto-generated method stub
		try {
			Object source = arg0.getSource();

			if (source == newProject) {

				if (!confirmDiscardChanges())
					return;

				editor.setProject(loader.newProject());
				SnapshotManager.clearAll();
				SnapshotManager.takeSnapshot();

			} else if (source == load) {

				if (!confirmDiscardChanges())
					return;

				String path = handler.getPath(editor.getProject());
				if (path != null) {
					// SnapshotManager
					// .setCurrentPerspectiveName(PerspectiveManager
					// .getDefaultPerspective());
					editor.setProject(loader.loadProject(path));
					SnapshotManager.clearAll();
					SnapshotManager.takeSnapshot();

				}

			} else if (source == save) {

				if (editor.getProject().getPath() != null) {
					save();
				} else {
					saveAs();
				}

			} else if (source == saveAs) {
				saveAs();

			}

		} catch (Throwable t) {
			t.printStackTrace();
			// JOptionPane.showMessageDialog(null, t.getMessage());
		}
	}

	protected boolean confirmDiscardChanges() {
		if (editor.getProject().isDirty()) {

			// JOptionPane.showMessageDialog(null, "Project is not saved",
			// "Alert", JOptionPane.ERROR_MESSAGE);

			int response = JOptionPane
					.showConfirmDialog(
							null,
							"This project has not been saved! If you continue, you will loose all changed data. Do you wish to continue ? ",
							"Attention", JOptionPane.YES_NO_OPTION);

			if (response == JOptionPane.YES_OPTION)
				return true;
			else
				return false;
		} else
			return true;
	}

	private void save() throws Throwable {
		loader.saveProject(editor.getProject());
		editor.getProject().setDirty(false);
		editor.refreshTitle();
	}

	private void saveAs() throws Throwable {
		String path = handler.getPath(editor.getProject());
		if (path != null) {
			editor.getProject().setPath(path);
			save();
		}
	}

	public MenuItem CreateMenuItem(String sText) {
		return CreateMenuItem(null, ITEM_PLAIN, sText, null, 0, null);
	}

	public MenuItem CreateMenuCheckItem(String sText) {
		return CreateMenuItem(null, ITEM_CHECK, sText, null, 0, null);
	}

	public MenuItem CreateMenuItem(Menu menu, int iType, String sText,
			Image image, int acceleratorKey, String sToolTip) {
		// Create the item
		MenuItem menuItem;

		switch (iType) {
		case ITEM_RADIO:
			menuItem = new RadioMenuItem(sText);
			break;

		case ITEM_CHECK:
			menuItem = new CheckMenuItem();
			break;

		default:
			menuItem = new MenuItem();
			break;
		}

		// Add the item test
		menuItem.setText(sText);

		// Add the optional icon
		if (image != null)
			menuItem.setGraphic(new ImageView(image));

		// Add the accelerator key
		if (acceleratorKey > 0) {
			// acceleratorKey
			// menuItem.setAccelerator(new KeyCombination());
		}

		// Add the optional tool tip text
		// if (sToolTip != null)
		// menuItem.setTooltip(sToolTip);

		// Add an action handler to this menu item
		// menuItem.addEventHandler(this);
		menuItem.setOnAction(this);

		if (menu != null)
			menu.getItems().add(menuItem);

		return menuItem;
	}

	protected Button createButton(String name) {
		Button c1 = new Button();
		c1.setText(name);
		// c1.setMargin(new Insets(0, 0, 0, 0));
		c1.addEventHandler(null, this);
		return c1;
	}

}
